<?php

class messageAction extends CommonAction
{

    public function  del()
    {
        $message =M("message");
        if (!$message->delete($_GET['id']))
        {
            $this->error("删除失败");
        } else
        {
            $this->success("删除成功");
        }
    }

    public function Recycle()
    {
        $Form = M("message");
        $flag = session("username");
        $data = $Form->where(" username = '" . $flag . "' AND DeletedByUsername = 1 OR sender = '" . $flag . "' AND DeletedBySender = 1")->select();

        //检验是否可用
        if ($data)
        {
            $this->assign('list', $data);
        }
        $this->display();
    }

    public function Outbox()
    {
        $Form = M("message");
        $conditon["sender"] = session("username");
        $conditon["isSend"] = 1;
        $conditon["DeletedBySender"] = 0;
        $data = $Form->where($conditon)->select();

        $this->emp_list=$this->CacheArray("employee");


        $this->assign('list', $data);
        $this->display();
    }

    public  function  inboxData()
    {
        $message = M("message");
        $conditon["username"] = I('session.userid');
        $conditon["DeletedUsername"] = 0;
        $conditon["isDraft"] = 0;
        $data = $message->where($conditon)->select();
        //检验是否可用
        return $data;
    }

    public function Inbox()
    {
        $username=$this->CacheArray("employee");
        $this->username=$username;

        $data = $this->inboxData();
        $this->assign('list', $data);
        $this->display();
    }
    
    public function ReplayList()
    {
    	$id=I("get.id");
    	
        $username=$this->CacheArray("employee");
        $this->username=$username;
        
        $message = M("message");
        $data = $message->where("id=$id")->select();
        $this->assign('list', $data);
        
        $replay=M("replay");
        $replayData=$replay->where("messageid=$id")->select();
        $this->assign('replay_list', $replayData);
        
        $this->display();
    }
    
     public function welcomeReplay()
    {
    	$id=I("get.id");  
        $replay=M("replay");
        $replayData=$replay->where("messageid=$id")->select();
        $this->assign('replay_list', $replayData);
        
        $this->display();
    }

    public function Reply()
    {
        if (IS_POST)
        {
            $data['recontent'] = I("post.recontent");
            $data["replytime"] = date("Y-m-d H:i:s");
            $data["messageid"]=I("post.id");
            $model=M("replay");
            if($model->add($data))
            {
                $this->success("回复成功");
            }
            else
            {
                $this->error("回复失败");
            }
        } else
        {
            $id = I("get.id");
            $model = M("message");
            $info = $model->find($id);
            $this->info = $info;
            $this->id=$id;
            $this->display();
        }

    }

    public function deleteOutbox()
    {
        $Form = M('message');
        $id = $_GET['id'];
        $data = array();
        $data['id'] = $id;
        $data['DeletedBySender'] = 1;
        $Form->save($data);
        $this->success('删除成功！' );
    }

    public function savemessage()
    {
        if (!$this->RoleAccess(MODULE_NAME,ACTION_NAME))
        {
            $this->error("你没有权限使用当前功能!");
        }
        $username = I("post.username", "");
        if (empty($username)) $this->error("参数传递出错");
        $userary = explode(",", $username);

        foreach ($userary as $id)
        {
            $message = D("message");
            // dump($_POST);exit();
            $Data = $message->create();
            $Data['sender'] = session("username");
            $Data['sendtime'] = date('Y-m-d H:i:s');

            if ($_POST['way'] == "submit")
            {
                $list = $message->add($Data);
                if ($list > 0)
                {
                    $this->success('数据保存成功！', 'Outbox');
                } else
                {
                    echo $message->getLastSql();
                    echo('数据写入错误！');
                }
            }
        }

    }

    //todo:完善用户能否使用当前模块
    private   function  RoleAccess($module='',$action='')
    {
        $myinfo = I("session.my_info");
        $role = $myinfo["role"];
        if ($role == 3 || $role == 9)
        {
            return true;
        } else
        {
            return false;
        }
    }


    public function newpage()
    {
        if (!$this->RoleAccess(MODULE_NAME,ACTION_NAME))
        {
            $this->error("你没有权限使用当前功能!");
        }

        $employee = M("employee");
        $conditon["isdelete"] = 0;
        $employeedata = $employee->field("username,name,id")->where($conditon)->order("id desc")->select();
        $this->userlist = $employeedata;

        $Form = M("message");
        $conditon["username"] = session("username");
        $Form->where($conditon)->select();


        $Form = M("employee");
        $list = $Form->order('id desc')->select();
        $this->assign('list', $list);
        $this->display();

    }

    public function _empty()
    {
        //空操作
        $this->error("您访问的页面不存在！");
    }

    //--------------------将消息标识为已读
    public function  read()
    {
        $message = M("message");
        $id = I("post.id");
        $data['isRead']=1;
        $data['readtime']=date('Y-m-d H:i:s');
        if (false === $message->where("id=" . $id)->save($data))
        {
            echo "error";
        }
        echo "success";
    }

    public function UnreadMessages()
    {
        $message = M("message");
        $conditon["username"] = I('session.userid');
        $conditon["isRead"]="0";
        $data=$message->where($conditon)->select();
        $this->num= count($data);
        $this->display();
    }
    
    public function GetUnreadMessagesNum()
    {
    	$message = M("message");
        $conditon["username"] = I('session.userid');
        $conditon["isRead"]="0";
        $data=$message->where($conditon)->count();
        echo  '{"status":"success","errMsg":"'.$data.'"}';
    }
    
}

?>